package com.tfswx.fljdmxyy.service.impl;

import cn.hutool.core.text.CharSequenceUtil;
import com.newbie.core.exception.BusinessException;
import com.tfswx.fljdmxyy.constant.SystemConstant;
import com.tfswx.fljdmxyy.dao.PzXtPzMapper;
import com.tfswx.fljdmxyy.dto.pzgl.*;
import com.tfswx.fljdmxyy.enums.BusinessCodeEnum;
import com.tfswx.fljdmxyy.enums.XtpzEnum;
import com.tfswx.fljdmxyy.model.PzXtPz;
import com.tfswx.fljdmxyy.query.pzgl.PzxlbQueryParam;
import com.tfswx.fljdmxyy.query.pzgl.PzxlbQueryResult;
import com.tfswx.fljdmxyy.service.PzglService;
import com.tfswx.fljdmxyy.utils.ConverterUtils;
import com.tfswx.newbie.boot.starter.page.PageHelper;
import com.tfswx.newbie.boot.starter.page.PageParam;
import com.tfswx.newbie.boot.starter.page.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 系统配置实现类
 *
 * @Author 陈文佳
 * @Date 2023/03/24
 */
@Service
public class PzglServiceImpl implements PzglService {

    @Resource
    private PzXtPzMapper pzXtPzMapper;

    @Override
    public PzxGetOutputDTO getPzx(PzxGetInputDTO inputDTO) {
        // 1.根据配置项编码查询配置项信息
        PzXtPz param = ConverterUtils.convert(inputDTO, PzXtPz.class);
        param.setSfsc(SystemConstant.N);
        PzXtPz pzXtPz = pzXtPzMapper.getPzXtPz(param);
        if (Objects.isNull(pzXtPz)) {
            throw new BusinessException(BusinessCodeEnum.CXSB.getCode(), "配置项不存在，配置项编码：" + param.getPzxbm());
        }

        // 2.将查询数据转换为出参
        return ConverterUtils.convert(pzXtPz, PzxGetOutputDTO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PzxAddOutputDTO addPzx(PzxAddInputDTO inputDTO) {
        // 1.判断配置项编码是否已存在
        PzXtPz queryParam = ConverterUtils.convert(inputDTO, PzXtPz.class);
        PzXtPz pz = pzXtPzMapper.getPzXtPz(queryParam);
        if (Objects.nonNull(pz) && CharSequenceUtil.equals(SystemConstant.N, pz.getSfsc())) {
            throw new BusinessException(BusinessCodeEnum.TJSB);
        }

        // 2.构造配置项信息
        PzXtPz param = ConverterUtils.convert(inputDTO, PzXtPz.class);
        Date date = new Date();
        param.setSfsc(SystemConstant.N);
        param.setCjsj(date);
        param.setZhxgsj(date);

        // 3.如果数据库存在已删除的此配置项编码，则恢复并更新删除；没有则新增
        if (Objects.nonNull(pz)) {
            pzXtPzMapper.updatePzx(pz);
        } else {
            pzXtPzMapper.insertPzXtPz(param);
        }

        return new PzxAddOutputDTO(inputDTO.getPzxbm());
    }

    @Override
    public Boolean updatePzx(PzxUpdateInputDTO inputDTO) {
        PzXtPz param = ConverterUtils.convert(inputDTO, PzXtPz.class);
        param.setZhxgsj(new Date());
        Integer num = pzXtPzMapper.updatePzXtPz(param);
        if (num < 1) {
            throw new BusinessException(BusinessCodeEnum.XGSB);
        }
        return Boolean.TRUE;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deletePzx(PzxDeleteInputDTO inputDTO) {
        inputDTO.getPzxbm().forEach(pzxbm -> {
            PzXtPz param = new PzXtPz();
            param.setPzxbm(pzxbm);
            param.setZhxgsj(new Date());
            Integer num = pzXtPzMapper.deletePzXtPz(param);
            if (num < 1) {
                throw new BusinessException(BusinessCodeEnum.SCSB);
            }
        });
        return Boolean.TRUE;
    }

    @Override
    public PageResult<PzxlbQueryResult> queryPzxlb(PageParam<PzxlbQueryParam> param) {
        return PageHelper.doSelectPage(param, () -> pzXtPzMapper.queryPzxlb(param.getFilter()));
    }

    @Override
    public List<PzxlbQueryResult> queryKfpzx() {
        PzxlbQueryParam param = new PzxlbQueryParam();
        param.setPzxlx(XtpzEnum.KF.getCode());
        return ConverterUtils.convertList(pzXtPzMapper.queryPzxlb(param), PzxlbQueryResult.class);
    }
}
